package monitor

import (
	"net/http"
	"strconv"

	"main/handlers"

	"github.com/gin-gonic/gin"
)

// ProjectHealthCalendar 处理获取告警数据的请求
func PortRunInfo(c *gin.Context) {
	projectIDStr := c.Query("project_id")
	projectID, err := strconv.Atoi(projectIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"})
		return
	}

	// 构建 SQL 查询
	query := `
		SELECT a.alarm_config_name,a.alarm_status,a.alarm_config_level_type,b.alarm_event_alarm_status,b.alarm_event_result_status,b.alarm_event_result_time
		FROM alarm_config a  
		LEFT JOIN alarm_event b ON a.alarm_config_id = b.alarm_config_id 
		LEFT JOIN alarm_config_group c ON a.alarm_config_group_id = c.alarm_config_group_id
		WHERE c.project_id = ?
		AND b.alarm_event_exec_status = 1
		AND b.alarm_event_mask != 1
		AND DATE_FORMAT(b.alarm_event_time, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')  
		GROUP BY a.alarm_config_id
		ORDER BY a.alarm_config_id DESC;
	`

	// 执行查询
	results, err := handlers.ExecuteQuery(query, projectID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Error executing query", "details": err.Error()})
		return
	}

	c.JSON(http.StatusOK, results) // 返回转换后的响应
}
